package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.UserVipService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.UserVipDao;
import com.qdlc.p2p.dal.dao.UserVipGrowthLogDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserVip;
import com.qdlc.p2p.dal.dto.UserVipLevel;
import com.qdlc.p2p.dal.model.UserVipGrowthLogModel;
import com.qdlc.p2p.dal.model.UserVipModel;
import com.qdlc.p2p.dal.util.UserUtils;

@Service
public class UserVipServiceImpl implements UserVipService {

    @Resource
    private UserVipDao userVipDao;

    @Resource
    private UserVipGrowthLogDao userVipGrowthLogDao;

    @Override
    public PageDataList<UserVipModel> getVipList(UserVipModel model) {
        QueryParam param = QueryParam.getInstance();
        if (StringUtil.isNotBlank(model.getSearchName())) {
            SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2);
        } else {
            if (StringUtil.isNotBlank(model.getUserName())) {
                param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
            }
            if (StringUtil.isNotBlank(model.getRealName())) {
                param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
            }
            if (StringUtil.isNotBlank(model.getStartTime())) {
                param.addParam("vipTime", Operators.GT, DateUtil.valueOf(model.getStartTime()));
            }
            if (StringUtil.isNotBlank(model.getEndTime())) {
                Date date = DateUtil.valueOf(model.getEndTime());
                param.addParam("vipTime", Operators.LT, DateUtil.rollDay(date, 1));
            }
            if (model.getVipLevel() != -1) {
                param.addParam("userVipLevel.level", model.getVipLevel());
            }
        }
        param.addPage(model.getPage(), model.getRows());
        PageDataList<UserVip> pageDataList = userVipDao.findPageList(param);
        PageDataList<UserVipModel> modelDataList = new PageDataList<UserVipModel>();
        List<UserVipModel> list = new ArrayList<UserVipModel>();
        modelDataList.setPage(pageDataList.getPage());
        for (UserVip vip : pageDataList.getList()) {
            UserVipModel uvm = UserVipModel.instance(vip);
            User user = UserUtils.getUserById(vip.getUserId());
            uvm.setUserName(user.getUserName());
            uvm.setRealName(user.getRealName());
            uvm.setUserId(vip.getUserId());
            UserVipLevel level = vip.getUserVipLevel();
            uvm.setExtraRate(level.getExtraRate());
            uvm.setVipLevel(level.getLevel());
            list.add(uvm);
        }
        modelDataList.setList(list);
        return modelDataList;
    }

    @Override
    public UserVip getUserVipById(long id) {
        return userVipDao.find(id);
    }

    @Override
    public PageDataList<UserVipGrowthLogModel> getUserVipGrowthLogListByUserId(long userId, int page, int size) {
        return userVipGrowthLogDao.getUserVipGrowthLogListByUserId(userId, page, size);
    }

}
